#'###########################################s
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())

source("C:/UQAR/Recherche/Maitrise/Donnees/R/Utils/statsUtils.R")
source("C:/UQAR/Recherche/Maitrise/Donnees/R/Utils/graphUtils.R")
source("C:/UQAR/Recherche/Maitrise/Donnees/R/Utils/utils.R")

WORKING_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Bylot"

#'#
#'# Chargement des donnees
#'# 

locsclasses <- c(3, 2, 1, 0, "A", "B")

static.data <- load.data(path=file.path(WORKING_DIR, "Fixe"), locsclasses=locsclasses)
mobile.data <- load.data(path=file.path(WORKING_DIR, "Mobile"), locsclasses=locsclasses)
fox.data <- load.data(path=file.path(WORKING_DIR, "Brutes/Argos/Bylot2012_renards_merged.txt"), locsclasses=locsclasses)


#'#
#'# Intervalle de confiance
#'#

static.CI <- disp.boot(static.data, FUN = stat.prop,
                      countBy=quote(list(LC, id)),
                      propBy=quote(list(id)),
                      statBy=quote(list(LC)),
                      key=c("LC"))
mobile.CI <- disp.boot(mobile.data, FUN = stat.prop,
                      countBy=quote(list(LC, id)),
                      propBy=quote(list(id)),
                      statBy=quote(list(LC)),
                      key=c("LC"))
fox.CI <- disp.boot(fox.data, FUN = stat.prop,
                    countBy=quote(list(LC, id)),
                    propBy=quote(list(id)),
                    statBy=quote(list(LC)),
                    key=c("LC"))
                  
#'#
#'# Ecart-type
#'#

static.SD <- disp.boot(static.data, FUN = stat.prop,
                      countBy=quote(list(LC, id)),
                      propBy=quote(list(id)),
                      statBy=quote(list(LC)),
                      key=c("LC"),
                      method="sd")
mobile.SD <- disp.boot(mobile.data, FUN = stat.prop,
                      countBy=quote(list(LC, id)),
                      propBy=quote(list(id)),
                      statBy=quote(list(LC)),
                      key=c("LC"),
                      method="sd")
fox.SD <- disp.boot(fox.data, FUN = stat.prop,
                    countBy=quote(list(LC, id)),
                    propBy=quote(list(id)),
                    statBy=quote(list(LC)),
                    key=c("LC"),
                    method="sd")


all <- rbind(static.CI[, type:= "1"], mobile.CI[, type := "2"] , fox.CI[, type := "3"])
setkey(all, LC, type)
all.filtered <- all[locsclasses]
all.filtered[, LC := factor(LC, levels=locsclasses)]

colors <- c("lightblue", "lightsteelblue", "darkblue")


#'#
#'# Barplot with CI
#'# 

png("C:/UQAR/Recherche/Maitrise/Donnees/Resultats/Repartitionslocs.png", 700, 700)
barchart(mean ~ LC, all.filtered, groups=as.factor(type), ly=all.filtered[,lb],
         uy=all.filtered[, ub], prepanel=prepanel.errorbar, panel=panel.errorbar,
         box.ratio=3, stack=FALSE, col=colors,
         key=list(text=list(c("Statique", "Mobile", "Renards")), 
                  rectangles=list(col=colors), x=1, y=0.98, corner=c(1, 1)),
         xlab="Classes de localisation",
         ylab="Proportion moyenne de localisations (% \261 95% CI)",
				 main="R\u00e9partition des localisations en fonction de la classe de localisation et du type de traitement",
				 addtext=TRUE,
				 textFUN=panel.text.addNlocs,
				 textlabels=c(paste("Nlocs statique =", nrow(static.data)), 
						 					paste("Nlocs mobile = ", nrow(mobile.data)),
											paste("Nlocs renards = ", nrow(fox.data))),
				 textadj=c(1,1),
				 spacing=0.05,
				 propy=0.75,
				 offx=0.3,
				 scales = list(col=1,tck=c(1,0))
         )
dev.off()


